
export default {
  data() {
    return {
      data: ['应出勤时长', '实际出勤时长', '加班时长'],
      operatorArr: ['+', '-', '×', '÷', '()'],
      FUNC: [{
        name: '四舍五入',
        type: 'ROUND',
        formula: '(a,b)',
        text: '四舍五入：按指定的位数对数值进行四舍五入.',
        grammar: '语法:ROUND(number,num) ▪ number: 要四舍五入的数值 ▪ num: 执行四舍五入时采用的位数。如果此参数为零，则圆整到最接近的整数；不支持负数. 举例:ROUND(9.75453,3)，则结果是 9.755'
      },
      {
        name: '求和',
        type: 'SUM',
        formula: '(a,b)',
        text: '求和：计算所有数值的和.',
        grammar: '语法:SUM(number1,number2,...) ▪ number1,number2,...  是 1 到 255 个待求和的数值参数. 举例: SUM(1,2,3,4,5,6)，则结果是 (1+2+3+4+5+6) = 21'
      },
      {
        name: '向上取整',
        type: 'ROUNDUP',
        formula: '(a,b)',
        text: '向上取整：朝着远离零的方向将数字进行向上舍入.',
        grammar: '语法: ROUNDUP(number,num) ▪ number:  必需，需要向上舍入的任意实数。▪ num: 必需，要将数字舍入到的位数，默认值是0，可选值为0、1、2，填入其他数值将会被视为公式不合法举例:ROUNDUP(87.9,0),则结果是88'
      },
      {
        name: '向下取整',
        type: 'ROUNDDOWN',
        formula: '(a,b)',
        text: '向下取整：朝着零的方向将数字进行向下舍入.',
        grammar: '语法:ROUNDDOWN(number, num) ▪ number:  必需，需要向下舍入的任意实数。 ▪ num: 必需，要将数字舍入到的位数，默认值是0，可选值为0、1、2，填入其他数值将会被视为公式不合法举例:ROUNDDOWN(9.3,0),则结果是9'
      },
      ],
      equation_index: -1,
      ISmessage: true,
      checkingIn: [],
      payment: [],
      resData: [],
      nameArr: ['应发工资', '实发工资', '应税工资']
    };
  },
  methods: {
    equationFun(index) {
      this.equation_index = index;
      this.equation.map((item, i) => {
        item.IsShow = index == i ? true : false;
      });
    },
    restore() {
      this.equation.map(item => {
        item.IsShow = false;
      });
    },
    addField(row, type) { // 添加公式字段
      // #age#+|price|*5+|c|-#name#+SUM(#c#+|d|)
      this.restore();
      let value = '';
      if (type == 1) {
        value = [{
          name: '#' + row.name + '#',
          value: '#' + row.namePy + '#',
          Name: row.name,
          NamePy: row.namePy,
          IsShow: true,
          type,
          number: Math.floor(Math.random() * (100 - 50)) + 50
        }];
      } else if (type == 2) {
        let isStat = this.nameArr.find(item=>{
          return item == row.name;
        });
        value = [{
          name: '#' + row.name + '#',
          value: isStat ? '[' + row.namePy + ']' : '|' + row.namePy + '|',
          Name: row.name,
          NamePy: row.namePy,
          IsShow: true,
          type,
          number: Math.floor(Math.random() * (100 - 50)) + 50
        }];
      } else if (type == 3) {
        value = [];
        value.push({
          name: row.type,
          value: row.type,
          formula: row.type,
          IsShow: false,
          type
        });
        let formula = '(a,b)'.split('', '(a,b)'.length);
        for (let index = 0; index < formula.length; index++) {
          const element = formula[index];
          value.push({
            name: element,
            value: element,
            IsShow: index == formula.length - 1 ? true : false,
            formula: row.type,
            type: 5
          });
        }
      } else if (type == 4) {
        if (row == '()') {
          value = [{
            name: '(',
            value: '(',
            IsShow: false,
            type: 5
          }, {
            name: ')',
            value: ')',
            IsShow: true,
            type: 5
          }];
        } else {
          value = [{
            name: row,
            value: row == '×' ? '*' : row == '÷' ? '/' : row,
            IsShow: true,
            type
          }];
        }
      }
      this.setValue(value);
    },
    keyboardMonitoring() { // 添加数值 | 运算符
      let that = this;
      document.onkeydown = function (event) {
        if (event) {
          let value = '';
          console.log(event.keyCode, 'event.keyCode');
          if (event.keyCode == 188) { // ,
            value = [{
              name: ',',
              value: ',',
              IsShow: true,
              type: 4,
            }];
          }else if (event.keyCode == 110 || event.keyCode == 190) { // .
            value = [{
              name: '.',
              value: '.',
              IsShow: true,
              type: 4,
            }];
          }else if (event.keyCode == 107 || event.shiftKey && event.keyCode == 187) { // +
            value = [{
              name: '+',
              value: '+',
              IsShow: true,
              type: 4,
            }];
          } else if (event.keyCode == 109 || event.keyCode == 189) { // -
            value = [{
              name: '-',
              value: '-',
              IsShow: true,
              type: 4,
            }];
          } else if (event.shiftKey && event.keyCode == 56 || event.keyCode == 106) { // ×
            value = [{
              name: '×',
              value: '*',
              IsShow: true,
              type: 4,
            }];
          } else if (event.keyCode == 111) { // ÷
            value = [{
              name: '÷',
              value: '/',
              IsShow: true,
              type: 4,
            }];
          } else if (event.shiftKey && event.keyCode == 57) { // (
            value = [{
              name: '(',
              value: '(',
              IsShow: true,
              type: 4,
            }];
          } else if (event.shiftKey && event.keyCode == 48) { // )
            value = [{
              name: ')',
              value: ')',
              IsShow: true,
              type: 4,
            }];
          } else if (event.keyCode == 96 || event.keyCode == 48) { // 0
            value = [{
              name: '0',
              value: 0,
              IsShow: true,
              type: 5,
            }];
          } else if (event.keyCode == 97 || event.keyCode == 49) { // 1
            value = [{
              name: '1',
              value: 1,
              IsShow: true,
              type: 5,
            }];
          } else if (event.keyCode == 98 || event.keyCode == 50) { // 2
            value = [{
              name: '2',
              value: 2,
              IsShow: true,
              type: 5,
            }];
          } else if (event.keyCode == 99 || event.keyCode == 51) { // 3
            value = [{
              name: '3',
              value: 3,
              IsShow: true,
              type: 5,
            }];
          } else if (event.keyCode == 100 || event.keyCode == 52) { // 4
            value = [{
              name: '4',
              value: 4,
              IsShow: true,
              type: 5,
            }];
          } else if (event.keyCode == 101 || event.keyCode == 53) { // 5
            value = [{
              name: '5',
              value: 5,
              IsShow: true,
              type: 5,
            }];
          } else if (event.keyCode == 102 || event.keyCode == 54) { // 6
            value = [{
              name: '6',
              value: 6,
              IsShow: true,
              type: 5,
            }];
          } else if (event.keyCode == 103 || event.keyCode == 55) { // 7
            value = [{
              name: '7',
              value: 7,
              IsShow: true,
              type: 5,
            }];
          } else if (event.keyCode == 104 || event.keyCode == 56) { // 8
            value = [{
              name: '8',
              value: 8,
              IsShow: true,
              type: 5,
            }];
          } else if (event.keyCode == 105 || event.keyCode == 57) { // 9
            value = [{
              name: '9',
              value: 9,
              IsShow: true,
              type: 5,
            }];
          } else if (event.keyCode == 8) { // 删除
            if (that.equation_index != -1) {
              that.equation.splice(that.equation_index, 1);
              let index = that.equation_index - 1;
              that.equation_index = index;
              that.equation[index] ? that.equation[index].IsShow = true : '';
            }
          } else if (event.keyCode == 37) { // 左
            if (that.equation_index != -1) {
              let index = that.equation_index - 1;
              that.restore();
              that.equation_index = index;
              that.equation[index] ? index == -1 ? that.equation[index].IsShow = false : that.equation[index].IsShow = true : '';
            }
          } else if (event.keyCode == 39) { // 右
            if (that.equation_index != that.equation.length - 1) {
              let index = that.equation_index + 1;
              that.restore();
              that.equation_index = index;
              that.equation[index] ? that.equation[index].IsShow = true : '';
            }
          } else if (!event.shiftKey) {
            if (that.ISmessage) {
              that.ISmessage = false;
              that.$message({
                type: 'warning',
                message: '请输入系统运算字符'
              });
              setTimeout(() => {
                that.ISmessage = true;
              }, 1500);
            }
          }
          that.setValue(value);
        }
      };
    },
    setValue(value) {
      if (value) {
        let index = this.equation_index + value.length;
        this.restore();
        this.equation.splice(index, 0, ...value);
        this.equation_index = index;
      }
    },
    getData() {
      this.$api.CustomFormula.GetCustomFormula().then(res=>{
        let resData = res.data.data;
        this.checkingIn = resData.filter(res=>{
          return res.type == 1;
        });
        this.payment = resData.filter(res=>{
          return res.type == 2;
        });
        this.resData = resData;
      });
    }
  },
  mounted () {
    this.getData();
  },
  activated() {
    this.keyboardMonitoring();
  },
  deactivated() { // 被 keep-alive 缓存的组件停用时调用
    document.onkeydown = undefined; // 销毁快捷键绑定
  },
};
